{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "colab_type": "text",
    "id": "4E92SIvUwTiS"
   },
   "source": [
    "## Install master branch of TensorTrade"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 0,
   "metadata": {
    "colab": {},
    "colab_type": "code",
    "id": "nmpEUkumvGB4"
   },
   "outputs": [],
   "source": [
    "!pip install git+https://github.com/tensortrade-org/tensortrade.git -U"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "colab": {},
    "colab_type": "code",
    "id": "1IsqDbMIpo_h"
   },
   "outputs": [],
   "source": [
    "import tensortrade.env.default as default\n",
    "\n",
    "from tensortrade.feed.core import Stream, DataFeed\n",
    "from tensortrade.data.cdd import CryptoDataDownload\n",
    "from tensortrade.oms.wallets import Portfolio, Wallet\n",
    "from tensortrade.oms.exchanges import Exchange\n",
    "from tensortrade.oms.services.execution.simulated import execute_order\n",
    "from tensortrade.oms.instruments import USD, BTC, ETH, LTC"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "colab_type": "text",
    "id": "5Y-6kHm3y5xR"
   },
   "source": [
    "## Load Data for Exchanges\n",
    "\n",
    "Using the `tensortrade.data.cdd` module you can load data from any `csv` file provided at:\n",
    "- https://www.cryptodatadownload.com/data/northamerican/\n",
    "\n",
    "using the `CryptoDataDownload` class."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "colab": {},
    "colab_type": "code",
    "id": "w3tUZOiqwgc5"
   },
   "outputs": [],
   "source": [
    "cdd = CryptoDataDownload()\n",
    "bitfinex_btc = cdd.fetch(\"Bitfinex\", \"USD\", \"BTC\", \"1h\")\n",
    "bitfinex_eth = cdd.fetch(\"Bitfinex\", \"USD\", \"ETH\", \"1h\")\n",
    "\n",
    "bitstamp_btc = cdd.fetch(\"Bitstamp\", \"USD\", \"BTC\", \"1h\")\n",
    "bitstamp_eth = cdd.fetch(\"Bitstamp\", \"USD\", \"ETH\", \"1h\")\n",
    "bitstamp_ltc = cdd.fetch(\"Bitstamp\", \"USD\", \"LTC\", \"1h\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "colab_type": "text",
    "id": "v7AQ9prfwDdk"
   },
   "source": [
    "## Inspect Transactions of `SimpleOrders`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/",
     "height": 436
    },
    "colab_type": "code",
    "id": "qb-WlDCquQ8b",
    "outputId": "38b47e55-ee3f-4fa5-92b9-0653bba5bf07"
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>poid</th>\n",
       "      <th>step</th>\n",
       "      <th>source</th>\n",
       "      <th>target</th>\n",
       "      <th>memo</th>\n",
       "      <th>amount</th>\n",
       "      <th>free</th>\n",
       "      <th>locked</th>\n",
       "      <th>locked_poid</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>ba218138-98c9-4e2c-b2fd-c12c79f2a07e</td>\n",
       "      <td>1</td>\n",
       "      <td>bitstamp:USD/free</td>\n",
       "      <td>bitstamp:USD/locked</td>\n",
       "      <td>LOCK (LOCK FOR ORDER)</td>\n",
       "      <td>1000.00 USD</td>\n",
       "      <td>0.00 USD</td>\n",
       "      <td>1000.00 USD</td>\n",
       "      <td>1000.00 USD</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>ba218138-98c9-4e2c-b2fd-c12c79f2a07e</td>\n",
       "      <td>1</td>\n",
       "      <td>bitstamp:USD/locked</td>\n",
       "      <td>bitstamp</td>\n",
       "      <td>WITHDRAWAL (COMMISSION)</td>\n",
       "      <td>3.00 USD</td>\n",
       "      <td>0.00 USD</td>\n",
       "      <td>997.00 USD</td>\n",
       "      <td>997.00 USD</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>ba218138-98c9-4e2c-b2fd-c12c79f2a07e</td>\n",
       "      <td>1</td>\n",
       "      <td>bitstamp:USD/locked</td>\n",
       "      <td>bitstamp</td>\n",
       "      <td>WITHDRAWAL (FILL ORDER)</td>\n",
       "      <td>997.00 USD</td>\n",
       "      <td>0.00 USD</td>\n",
       "      <td>0.00 USD</td>\n",
       "      <td>0.00 USD</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>ba218138-98c9-4e2c-b2fd-c12c79f2a07e</td>\n",
       "      <td>1</td>\n",
       "      <td>bitstamp</td>\n",
       "      <td>bitstamp:BTC/locked</td>\n",
       "      <td>DEPOSIT (TRADED 997.00 USD bitstamp:USD/BTC @ ...</td>\n",
       "      <td>0.10940979 BTC</td>\n",
       "      <td>5.00000000 BTC</td>\n",
       "      <td>0.10940979 BTC</td>\n",
       "      <td>0.10940979 BTC</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>ba218138-98c9-4e2c-b2fd-c12c79f2a07e</td>\n",
       "      <td>1</td>\n",
       "      <td>bitstamp:USD/locked</td>\n",
       "      <td>bitstamp:USD/free</td>\n",
       "      <td>UNLOCK USD (COMPLETED)</td>\n",
       "      <td>0.00 USD</td>\n",
       "      <td>0.00 USD</td>\n",
       "      <td>0.00 USD</td>\n",
       "      <td>0.00 USD</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>ba218138-98c9-4e2c-b2fd-c12c79f2a07e</td>\n",
       "      <td>1</td>\n",
       "      <td>bitstamp:BTC/locked</td>\n",
       "      <td>bitstamp:BTC/free</td>\n",
       "      <td>UNLOCK BTC (COMPLETED)</td>\n",
       "      <td>0.10940979 BTC</td>\n",
       "      <td>5.10940979 BTC</td>\n",
       "      <td>0.00000000 BTC</td>\n",
       "      <td>0.00000000 BTC</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>9ba20790-6b44-4f65-8cc3-545528dda301</td>\n",
       "      <td>2</td>\n",
       "      <td>bitfinex:BTC/free</td>\n",
       "      <td>bitfinex:BTC/locked</td>\n",
       "      <td>LOCK (LOCK FOR ORDER)</td>\n",
       "      <td>3.00000000 BTC</td>\n",
       "      <td>7.00000000 BTC</td>\n",
       "      <td>3.00000000 BTC</td>\n",
       "      <td>3.00000000 BTC</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                                   poid  step               source  \\\n",
       "0  ba218138-98c9-4e2c-b2fd-c12c79f2a07e     1    bitstamp:USD/free   \n",
       "1  ba218138-98c9-4e2c-b2fd-c12c79f2a07e     1  bitstamp:USD/locked   \n",
       "2  ba218138-98c9-4e2c-b2fd-c12c79f2a07e     1  bitstamp:USD/locked   \n",
       "3  ba218138-98c9-4e2c-b2fd-c12c79f2a07e     1             bitstamp   \n",
       "4  ba218138-98c9-4e2c-b2fd-c12c79f2a07e     1  bitstamp:USD/locked   \n",
       "5  ba218138-98c9-4e2c-b2fd-c12c79f2a07e     1  bitstamp:BTC/locked   \n",
       "6  9ba20790-6b44-4f65-8cc3-545528dda301     2    bitfinex:BTC/free   \n",
       "\n",
       "                target                                               memo  \\\n",
       "0  bitstamp:USD/locked                              LOCK (LOCK FOR ORDER)   \n",
       "1             bitstamp                            WITHDRAWAL (COMMISSION)   \n",
       "2             bitstamp                            WITHDRAWAL (FILL ORDER)   \n",
       "3  bitstamp:BTC/locked  DEPOSIT (TRADED 997.00 USD bitstamp:USD/BTC @ ...   \n",
       "4    bitstamp:USD/free                             UNLOCK USD (COMPLETED)   \n",
       "5    bitstamp:BTC/free                             UNLOCK BTC (COMPLETED)   \n",
       "6  bitfinex:BTC/locked                              LOCK (LOCK FOR ORDER)   \n",
       "\n",
       "           amount            free          locked     locked_poid  \n",
       "0     1000.00 USD        0.00 USD     1000.00 USD     1000.00 USD  \n",
       "1        3.00 USD        0.00 USD      997.00 USD      997.00 USD  \n",
       "2      997.00 USD        0.00 USD        0.00 USD        0.00 USD  \n",
       "3  0.10940979 BTC  5.00000000 BTC  0.10940979 BTC  0.10940979 BTC  \n",
       "4        0.00 USD        0.00 USD        0.00 USD        0.00 USD  \n",
       "5  0.10940979 BTC  5.10940979 BTC  0.00000000 BTC  0.00000000 BTC  \n",
       "6  3.00000000 BTC  7.00000000 BTC  3.00000000 BTC  3.00000000 BTC  "
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "bitfinex = Exchange(\"bitfinex\", service=execute_order)(\n",
    "    Stream.source(list(bitfinex_btc['close'][-100:]), dtype=\"float\").rename(\"USD-BTC\"),\n",
    "    Stream.source(list(bitfinex_eth['close'][-100:]), dtype=\"float\").rename(\"USD-ETH\")\n",
    ")\n",
    "\n",
    "bitstamp = Exchange(\"bitstamp\", service=execute_order)(\n",
    "    Stream.source(list(bitstamp_btc['close'][-100:]), dtype=\"float\").rename(\"USD-BTC\"),\n",
    "    Stream.source(list(bitstamp_eth['close'][-100:]), dtype=\"float\").rename(\"USD-ETH\"),\n",
    "    Stream.source(list(bitstamp_ltc['close'][-100:]), dtype=\"float\").rename(\"USD-LTC\")\n",
    ")\n",
    "\n",
    "portfolio = Portfolio(USD, [\n",
    "    Wallet(bitfinex, 10000 * USD),\n",
    "    Wallet(bitfinex, 10 * BTC),\n",
    "    Wallet(bitfinex, 5 * ETH),\n",
    "    Wallet(bitstamp, 1000 * USD),\n",
    "    Wallet(bitstamp, 5 * BTC),\n",
    "    Wallet(bitstamp, 20 * ETH),\n",
    "    Wallet(bitstamp, 3 * LTC)\n",
    "])\n",
    "\n",
    "feed = DataFeed([\n",
    "    Stream.source(list(bitstamp_eth['volume'][-100:]), dtype=\"float\").rename(\"volume:/USD-ETH\"),\n",
    "    Stream.source(list(bitstamp_ltc['volume'][-100:]), dtype=\"float\").rename(\"volume:/USD-LTC\")\n",
    "])\n",
    "\n",
    "env = default.create(\n",
    "    portfolio=portfolio,\n",
    "    action_scheme=default.actions.SimpleOrders(),\n",
    "    reward_scheme=default.rewards.SimpleProfit(),\n",
    "    feed=feed\n",
    ")\n",
    "\n",
    "done = False\n",
    "obs = env.reset()\n",
    "while not done:\n",
    "    action = env.action_space.sample()\n",
    "    obs, reward, done, info = env.step(action)\n",
    "\n",
    "portfolio.ledger.as_frame().head(7)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "colab_type": "text",
    "id": "WBE1dIhIwmTR"
   },
   "source": [
    "## Inspect Transactions of `ManagedRiskOrders`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/",
     "height": 1000
    },
    "colab_type": "code",
    "id": "nHX0gAahvY3g",
    "outputId": "496452e2-a6e6-4923-b2fd-8097ec8d9b9c"
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>poid</th>\n",
       "      <th>step</th>\n",
       "      <th>source</th>\n",
       "      <th>target</th>\n",
       "      <th>memo</th>\n",
       "      <th>amount</th>\n",
       "      <th>free</th>\n",
       "      <th>locked</th>\n",
       "      <th>locked_poid</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>ba218138-98c9-4e2c-b2fd-c12c79f2a07e</td>\n",
       "      <td>1</td>\n",
       "      <td>bitstamp:USD/free</td>\n",
       "      <td>bitstamp:USD/locked</td>\n",
       "      <td>LOCK (LOCK FOR ORDER)</td>\n",
       "      <td>1000.00 USD</td>\n",
       "      <td>0.00 USD</td>\n",
       "      <td>1000.00 USD</td>\n",
       "      <td>1000.00 USD</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>ba218138-98c9-4e2c-b2fd-c12c79f2a07e</td>\n",
       "      <td>1</td>\n",
       "      <td>bitstamp:USD/locked</td>\n",
       "      <td>bitstamp</td>\n",
       "      <td>WITHDRAWAL (COMMISSION)</td>\n",
       "      <td>3.00 USD</td>\n",
       "      <td>0.00 USD</td>\n",
       "      <td>997.00 USD</td>\n",
       "      <td>997.00 USD</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>ba218138-98c9-4e2c-b2fd-c12c79f2a07e</td>\n",
       "      <td>1</td>\n",
       "      <td>bitstamp:USD/locked</td>\n",
       "      <td>bitstamp</td>\n",
       "      <td>WITHDRAWAL (FILL ORDER)</td>\n",
       "      <td>997.00 USD</td>\n",
       "      <td>0.00 USD</td>\n",
       "      <td>0.00 USD</td>\n",
       "      <td>0.00 USD</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>ba218138-98c9-4e2c-b2fd-c12c79f2a07e</td>\n",
       "      <td>1</td>\n",
       "      <td>bitstamp</td>\n",
       "      <td>bitstamp:BTC/locked</td>\n",
       "      <td>DEPOSIT (TRADED 997.00 USD bitstamp:USD/BTC @ ...</td>\n",
       "      <td>0.10940979 BTC</td>\n",
       "      <td>5.00000000 BTC</td>\n",
       "      <td>0.10940979 BTC</td>\n",
       "      <td>0.10940979 BTC</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>ba218138-98c9-4e2c-b2fd-c12c79f2a07e</td>\n",
       "      <td>1</td>\n",
       "      <td>bitstamp:USD/locked</td>\n",
       "      <td>bitstamp:USD/free</td>\n",
       "      <td>UNLOCK USD (COMPLETED)</td>\n",
       "      <td>0.00 USD</td>\n",
       "      <td>0.00 USD</td>\n",
       "      <td>0.00 USD</td>\n",
       "      <td>0.00 USD</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>ba218138-98c9-4e2c-b2fd-c12c79f2a07e</td>\n",
       "      <td>1</td>\n",
       "      <td>bitstamp:BTC/locked</td>\n",
       "      <td>bitstamp:BTC/free</td>\n",
       "      <td>UNLOCK BTC (COMPLETED)</td>\n",
       "      <td>0.10940979 BTC</td>\n",
       "      <td>5.10940979 BTC</td>\n",
       "      <td>0.00000000 BTC</td>\n",
       "      <td>0.00000000 BTC</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>9ba20790-6b44-4f65-8cc3-545528dda301</td>\n",
       "      <td>2</td>\n",
       "      <td>bitfinex:BTC/free</td>\n",
       "      <td>bitfinex:BTC/locked</td>\n",
       "      <td>LOCK (LOCK FOR ORDER)</td>\n",
       "      <td>3.00000000 BTC</td>\n",
       "      <td>7.00000000 BTC</td>\n",
       "      <td>3.00000000 BTC</td>\n",
       "      <td>3.00000000 BTC</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>9ba20790-6b44-4f65-8cc3-545528dda301</td>\n",
       "      <td>2</td>\n",
       "      <td>bitfinex:BTC/locked</td>\n",
       "      <td>bitfinex</td>\n",
       "      <td>WITHDRAWAL (COMMISSION)</td>\n",
       "      <td>0.00900000 BTC</td>\n",
       "      <td>7.00000000 BTC</td>\n",
       "      <td>2.99100000 BTC</td>\n",
       "      <td>2.99100000 BTC</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>9ba20790-6b44-4f65-8cc3-545528dda301</td>\n",
       "      <td>2</td>\n",
       "      <td>bitfinex:BTC/locked</td>\n",
       "      <td>bitfinex</td>\n",
       "      <td>WITHDRAWAL (FILL ORDER)</td>\n",
       "      <td>2.99100000 BTC</td>\n",
       "      <td>7.00000000 BTC</td>\n",
       "      <td>0.00000000 BTC</td>\n",
       "      <td>0.00000000 BTC</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>9ba20790-6b44-4f65-8cc3-545528dda301</td>\n",
       "      <td>2</td>\n",
       "      <td>bitfinex</td>\n",
       "      <td>bitfinex:USD/locked</td>\n",
       "      <td>DEPOSIT (TRADED 2.99100000 BTC bitfinex:USD/BT...</td>\n",
       "      <td>27249.98 USD</td>\n",
       "      <td>10000.00 USD</td>\n",
       "      <td>27249.98 USD</td>\n",
       "      <td>27249.98 USD</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>10</th>\n",
       "      <td>9ba20790-6b44-4f65-8cc3-545528dda301</td>\n",
       "      <td>2</td>\n",
       "      <td>bitfinex:USD/locked</td>\n",
       "      <td>bitfinex:USD/free</td>\n",
       "      <td>UNLOCK USD (COMPLETED)</td>\n",
       "      <td>27249.98 USD</td>\n",
       "      <td>37249.98 USD</td>\n",
       "      <td>0.00 USD</td>\n",
       "      <td>0.00 USD</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>11</th>\n",
       "      <td>9ba20790-6b44-4f65-8cc3-545528dda301</td>\n",
       "      <td>2</td>\n",
       "      <td>bitfinex:BTC/locked</td>\n",
       "      <td>bitfinex:BTC/free</td>\n",
       "      <td>UNLOCK BTC (COMPLETED)</td>\n",
       "      <td>0.00000000 BTC</td>\n",
       "      <td>7.00000000 BTC</td>\n",
       "      <td>0.00000000 BTC</td>\n",
       "      <td>0.00000000 BTC</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>12</th>\n",
       "      <td>bc01cc16-082f-44ab-8ec9-9599360caa4d</td>\n",
       "      <td>3</td>\n",
       "      <td>bitfinex:ETH/free</td>\n",
       "      <td>bitfinex:ETH/locked</td>\n",
       "      <td>LOCK (LOCK FOR ORDER)</td>\n",
       "      <td>4.00000000 ETH</td>\n",
       "      <td>1.00000000 ETH</td>\n",
       "      <td>4.00000000 ETH</td>\n",
       "      <td>4.00000000 ETH</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>13</th>\n",
       "      <td>bc01cc16-082f-44ab-8ec9-9599360caa4d</td>\n",
       "      <td>3</td>\n",
       "      <td>bitfinex:ETH/locked</td>\n",
       "      <td>bitfinex</td>\n",
       "      <td>WITHDRAWAL (COMMISSION)</td>\n",
       "      <td>0.01200000 ETH</td>\n",
       "      <td>1.00000000 ETH</td>\n",
       "      <td>3.98800000 ETH</td>\n",
       "      <td>3.98800000 ETH</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>14</th>\n",
       "      <td>bc01cc16-082f-44ab-8ec9-9599360caa4d</td>\n",
       "      <td>3</td>\n",
       "      <td>bitfinex:ETH/locked</td>\n",
       "      <td>bitfinex</td>\n",
       "      <td>WITHDRAWAL (FILL ORDER)</td>\n",
       "      <td>3.98800000 ETH</td>\n",
       "      <td>1.00000000 ETH</td>\n",
       "      <td>0.00000000 ETH</td>\n",
       "      <td>0.00000000 ETH</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>15</th>\n",
       "      <td>bc01cc16-082f-44ab-8ec9-9599360caa4d</td>\n",
       "      <td>3</td>\n",
       "      <td>bitfinex</td>\n",
       "      <td>bitfinex:USD/locked</td>\n",
       "      <td>DEPOSIT (TRADED 3.98800000 ETH bitfinex:USD/ET...</td>\n",
       "      <td>920.39 USD</td>\n",
       "      <td>37249.98 USD</td>\n",
       "      <td>920.39 USD</td>\n",
       "      <td>920.39 USD</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>16</th>\n",
       "      <td>bc01cc16-082f-44ab-8ec9-9599360caa4d</td>\n",
       "      <td>3</td>\n",
       "      <td>bitfinex:USD/locked</td>\n",
       "      <td>bitfinex:USD/free</td>\n",
       "      <td>UNLOCK USD (COMPLETED)</td>\n",
       "      <td>920.39 USD</td>\n",
       "      <td>38170.37 USD</td>\n",
       "      <td>0.00 USD</td>\n",
       "      <td>0.00 USD</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>17</th>\n",
       "      <td>bc01cc16-082f-44ab-8ec9-9599360caa4d</td>\n",
       "      <td>3</td>\n",
       "      <td>bitfinex:ETH/locked</td>\n",
       "      <td>bitfinex:ETH/free</td>\n",
       "      <td>UNLOCK ETH (COMPLETED)</td>\n",
       "      <td>0.00000000 ETH</td>\n",
       "      <td>1.00000000 ETH</td>\n",
       "      <td>0.00000000 ETH</td>\n",
       "      <td>0.00000000 ETH</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>18</th>\n",
       "      <td>2f91580e-6da9-4e4d-9fc1-cc26eed1a65f</td>\n",
       "      <td>4</td>\n",
       "      <td>bitfinex:BTC/free</td>\n",
       "      <td>bitfinex:BTC/locked</td>\n",
       "      <td>LOCK (LOCK FOR ORDER)</td>\n",
       "      <td>0.70000000 BTC</td>\n",
       "      <td>6.30000000 BTC</td>\n",
       "      <td>0.70000000 BTC</td>\n",
       "      <td>0.70000000 BTC</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>19</th>\n",
       "      <td>2f91580e-6da9-4e4d-9fc1-cc26eed1a65f</td>\n",
       "      <td>4</td>\n",
       "      <td>bitfinex:BTC/locked</td>\n",
       "      <td>bitfinex</td>\n",
       "      <td>WITHDRAWAL (COMMISSION)</td>\n",
       "      <td>0.00210000 BTC</td>\n",
       "      <td>6.30000000 BTC</td>\n",
       "      <td>0.69790000 BTC</td>\n",
       "      <td>0.69790000 BTC</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                                    poid  step               source  \\\n",
       "0   ba218138-98c9-4e2c-b2fd-c12c79f2a07e     1    bitstamp:USD/free   \n",
       "1   ba218138-98c9-4e2c-b2fd-c12c79f2a07e     1  bitstamp:USD/locked   \n",
       "2   ba218138-98c9-4e2c-b2fd-c12c79f2a07e     1  bitstamp:USD/locked   \n",
       "3   ba218138-98c9-4e2c-b2fd-c12c79f2a07e     1             bitstamp   \n",
       "4   ba218138-98c9-4e2c-b2fd-c12c79f2a07e     1  bitstamp:USD/locked   \n",
       "5   ba218138-98c9-4e2c-b2fd-c12c79f2a07e     1  bitstamp:BTC/locked   \n",
       "6   9ba20790-6b44-4f65-8cc3-545528dda301     2    bitfinex:BTC/free   \n",
       "7   9ba20790-6b44-4f65-8cc3-545528dda301     2  bitfinex:BTC/locked   \n",
       "8   9ba20790-6b44-4f65-8cc3-545528dda301     2  bitfinex:BTC/locked   \n",
       "9   9ba20790-6b44-4f65-8cc3-545528dda301     2             bitfinex   \n",
       "10  9ba20790-6b44-4f65-8cc3-545528dda301     2  bitfinex:USD/locked   \n",
       "11  9ba20790-6b44-4f65-8cc3-545528dda301     2  bitfinex:BTC/locked   \n",
       "12  bc01cc16-082f-44ab-8ec9-9599360caa4d     3    bitfinex:ETH/free   \n",
       "13  bc01cc16-082f-44ab-8ec9-9599360caa4d     3  bitfinex:ETH/locked   \n",
       "14  bc01cc16-082f-44ab-8ec9-9599360caa4d     3  bitfinex:ETH/locked   \n",
       "15  bc01cc16-082f-44ab-8ec9-9599360caa4d     3             bitfinex   \n",
       "16  bc01cc16-082f-44ab-8ec9-9599360caa4d     3  bitfinex:USD/locked   \n",
       "17  bc01cc16-082f-44ab-8ec9-9599360caa4d     3  bitfinex:ETH/locked   \n",
       "18  2f91580e-6da9-4e4d-9fc1-cc26eed1a65f     4    bitfinex:BTC/free   \n",
       "19  2f91580e-6da9-4e4d-9fc1-cc26eed1a65f     4  bitfinex:BTC/locked   \n",
       "\n",
       "                 target                                               memo  \\\n",
       "0   bitstamp:USD/locked                              LOCK (LOCK FOR ORDER)   \n",
       "1              bitstamp                            WITHDRAWAL (COMMISSION)   \n",
       "2              bitstamp                            WITHDRAWAL (FILL ORDER)   \n",
       "3   bitstamp:BTC/locked  DEPOSIT (TRADED 997.00 USD bitstamp:USD/BTC @ ...   \n",
       "4     bitstamp:USD/free                             UNLOCK USD (COMPLETED)   \n",
       "5     bitstamp:BTC/free                             UNLOCK BTC (COMPLETED)   \n",
       "6   bitfinex:BTC/locked                              LOCK (LOCK FOR ORDER)   \n",
       "7              bitfinex                            WITHDRAWAL (COMMISSION)   \n",
       "8              bitfinex                            WITHDRAWAL (FILL ORDER)   \n",
       "9   bitfinex:USD/locked  DEPOSIT (TRADED 2.99100000 BTC bitfinex:USD/BT...   \n",
       "10    bitfinex:USD/free                             UNLOCK USD (COMPLETED)   \n",
       "11    bitfinex:BTC/free                             UNLOCK BTC (COMPLETED)   \n",
       "12  bitfinex:ETH/locked                              LOCK (LOCK FOR ORDER)   \n",
       "13             bitfinex                            WITHDRAWAL (COMMISSION)   \n",
       "14             bitfinex                            WITHDRAWAL (FILL ORDER)   \n",
       "15  bitfinex:USD/locked  DEPOSIT (TRADED 3.98800000 ETH bitfinex:USD/ET...   \n",
       "16    bitfinex:USD/free                             UNLOCK USD (COMPLETED)   \n",
       "17    bitfinex:ETH/free                             UNLOCK ETH (COMPLETED)   \n",
       "18  bitfinex:BTC/locked                              LOCK (LOCK FOR ORDER)   \n",
       "19             bitfinex                            WITHDRAWAL (COMMISSION)   \n",
       "\n",
       "            amount            free          locked     locked_poid  \n",
       "0      1000.00 USD        0.00 USD     1000.00 USD     1000.00 USD  \n",
       "1         3.00 USD        0.00 USD      997.00 USD      997.00 USD  \n",
       "2       997.00 USD        0.00 USD        0.00 USD        0.00 USD  \n",
       "3   0.10940979 BTC  5.00000000 BTC  0.10940979 BTC  0.10940979 BTC  \n",
       "4         0.00 USD        0.00 USD        0.00 USD        0.00 USD  \n",
       "5   0.10940979 BTC  5.10940979 BTC  0.00000000 BTC  0.00000000 BTC  \n",
       "6   3.00000000 BTC  7.00000000 BTC  3.00000000 BTC  3.00000000 BTC  \n",
       "7   0.00900000 BTC  7.00000000 BTC  2.99100000 BTC  2.99100000 BTC  \n",
       "8   2.99100000 BTC  7.00000000 BTC  0.00000000 BTC  0.00000000 BTC  \n",
       "9     27249.98 USD    10000.00 USD    27249.98 USD    27249.98 USD  \n",
       "10    27249.98 USD    37249.98 USD        0.00 USD        0.00 USD  \n",
       "11  0.00000000 BTC  7.00000000 BTC  0.00000000 BTC  0.00000000 BTC  \n",
       "12  4.00000000 ETH  1.00000000 ETH  4.00000000 ETH  4.00000000 ETH  \n",
       "13  0.01200000 ETH  1.00000000 ETH  3.98800000 ETH  3.98800000 ETH  \n",
       "14  3.98800000 ETH  1.00000000 ETH  0.00000000 ETH  0.00000000 ETH  \n",
       "15      920.39 USD    37249.98 USD      920.39 USD      920.39 USD  \n",
       "16      920.39 USD    38170.37 USD        0.00 USD        0.00 USD  \n",
       "17  0.00000000 ETH  1.00000000 ETH  0.00000000 ETH  0.00000000 ETH  \n",
       "18  0.70000000 BTC  6.30000000 BTC  0.70000000 BTC  0.70000000 BTC  \n",
       "19  0.00210000 BTC  6.30000000 BTC  0.69790000 BTC  0.69790000 BTC  "
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "portfolio = Portfolio(USD, [\n",
    "    Wallet(bitfinex, 10000 * USD),\n",
    "    Wallet(bitfinex, 0 * BTC),\n",
    "    Wallet(bitfinex, 0 * ETH),\n",
    "])\n",
    "\n",
    "env = default.create(\n",
    "    portfolio=portfolio,\n",
    "    action_scheme=default.actions.ManagedRiskOrders(),\n",
    "    reward_scheme=default.rewards.SimpleProfit(),\n",
    "    feed=feed\n",
    ")\n",
    "\n",
    "done = False\n",
    "\n",
    "while not done:\n",
    "    action = env.action_space.sample()\n",
    "    obs, reward, done, info = env.step(action)\n",
    "\n",
    "portfolio.ledger.as_frame().head(20)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "colab_type": "text",
    "id": "LZtj0_TsxLSl"
   },
   "source": [
    "## Transactions in Spreadsheets\n",
    "\n",
    "To take a closer look at the transactions that are happening within the system, copy the transactions to a csv file and load it into any spreadsheet software. If where you are running this allows access to the system clipboard, you can directly copy the frame to your system clipboard by doing the following:\n",
    "- `portfolio.ledger.as_frame().to_clipboard(index=False)`\n",
    "\n",
    "Then just paste into any spreadsheet software (e.g. Execel, Google Sheets)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "colab_type": "text",
    "id": "Pp9U6zaSyuDa"
   },
   "source": [
    "![image.png]()"
   ]
  }
 ],
 "metadata": {
  "colab": {
   "name": "Copy of ledger_example.ipynb",
   "provenance": []
  },
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
